View Javadoc
1   package edu.jiangxin.apktoolbox.convert.encoding;
2   
3   import edu.jiangxin.apktoolbox.swing.extend.EasyPanel;
4   import edu.jiangxin.apktoolbox.utils.Constants;
5   
6   import javax.swing.*;
7   import javax.swing.table.DefaultTableModel;
8   import java.awt.*;
9   import java.io.Serial;
10  
11  public class GarbledTextRecoveryPanel extends EasyPanel {
12      @Serial
13      private static final long serialVersionUID = 1L;
14  
15      private JPanel inputPanel;
16  
17      private JTextArea inputArea;
18  
19      private JPanel resultPanel;
20  
21      private DefaultTableModel tableModel;
22  
23      private JPanel operationPanel;
24  
25      private static final String[] CHARSETS = {
26              "UTF-8", "GBK", "ISO-8859-1", "Big5", "Shift_JIS", "EUC-KR", "Windows-1252"
27      };
28  
29      public GarbledTextRecoveryPanel() throws HeadlessException {
30          super();
31      }
32  
33      @Override
34      public void initUI() {
35          setPreferredSize(new Dimension(900, 600));
36          setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
37  
38          createInputPanel();
39          add(inputPanel);
40          add(Box.createVerticalStrut(Constants.DEFAULT_Y_BORDER));
41  
42          createResultPanel();
43          add(resultPanel);
44          add(Box.createVerticalStrut(Constants.DEFAULT_Y_BORDER));
45  
46          createOperationPanel();
47          add(operationPanel);
48      }
49  
50      private void createInputPanel() {
51          inputPanel = new JPanel();
52          BoxLayout layout = new BoxLayout(inputPanel, BoxLayout.X_AXIS);
53          inputPanel.setLayout(layout);
54  
55          inputArea = new JTextArea(4, 60);
56          inputArea.setText("鐢变簬鍦ㄧ被璺\uE21A緞涓\uE15E彂鐜颁簡涓€涓\uE045垨澶氫釜澶勭悊绋嬪簭锛屽洜姝ゅ惎鐢ㄤ簡");
57          JScrollPane inputScroll = new JScrollPane(inputArea);
58          inputScroll.setBorder(BorderFactory.createTitledBorder("Please enter the garbled text:"));
59          inputScroll.setPreferredSize(new Dimension(700, 200));
60  
61          inputPanel.add(inputScroll);
62      }
63  
64      private void createResultPanel() {
65          resultPanel = new JPanel();
66          BoxLayout layout = new BoxLayout(resultPanel, BoxLayout.Y_AXIS);
67          resultPanel.setLayout(layout);
68  
69  
70          String[] columns = {"Current Encoding", "Original Encoding", "Recovered Result"};
71          tableModel = new DefaultTableModel(columns, 0);
72          JTable resultTable = new JTable(tableModel);
73          resultTable.setRowHeight(30);
74          resultTable.getColumnModel().getColumn(2).setPreferredWidth(400);
75          JScrollPane tableScroll = new JScrollPane(resultTable);
76          tableScroll.setPreferredSize(new Dimension(700, 300));
77  
78          resultPanel.add(tableScroll);
79      }
80  
81      private void createOperationPanel() {
82          operationPanel = new JPanel();
83          BoxLayout layout = new BoxLayout(operationPanel, BoxLayout.X_AXIS);
84          operationPanel.setLayout(layout);
85  
86          JButton recoverButton = new JButton("Try All Encoding Combinations");
87          recoverButton.addActionListener(e -> recoverAll());
88  
89          operationPanel.add(recoverButton);
90      }
91  
92      private void recoverAll() {
93          tableModel.setRowCount(0);
94          String input = inputArea.getText().trim();
95          if (input.isEmpty()) return;
96  
97          for (String now : CHARSETS) {
98              for (String orig : CHARSETS) {
99                  if (!now.equals(orig)) {
100                     try {
101                         byte[] bytes = input.getBytes(now);
102                         String recovered = new String(bytes, orig);
103 
104                         tableModel.addRow(new Object[]{now, orig, recovered});
105                     } catch (Exception ex) {
106                         tableModel.addRow(new Object[]{now, orig, "Conversion Failed"});
107                     }
108                 }
109 
110             }
111         }
112     }
113 }